package com.health.memberbackstage.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.health.memberbackstage.pojo.*;
import com.health.memberbackstage.pojo.R.Result;
import com.health.memberbackstage.service.*;
import com.health.memberbackstage.utils.AuthenticationUtils;
import com.health.memberbackstage.utils.SnowflakeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrdersettingService ordersettingService;
    @Autowired
    private ReportService reportService;
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    @Transactional
    public synchronized Result add(@RequestBody Appointment appointment){

        Long memberID = AuthenticationUtils.getMemberID();

        String idcard = appointment.getIdcard();
        Member member = memberService.findByIdCard(idcard);
        if (member != null){

            Order order = new Order();
            order.setName(appointment.getName());
            order.setMemberId(memberID);
            order.setOrderdate(appointment.getHealthDate());
            order.setOrderType("微信预约");
            order.setSetmealId(appointment.getSetmealId());
            order.setMaritalStatus(appointment.getMaritalStatus());
            boolean orderFlag = orderService.addOrder(order);

            if (orderFlag){

                Report report = new Report();
                report.setMemberId(memberID);
                report.setName(appointment.getName());
                report.setSex(appointment.getSex());
                report.setHealthDate(appointment.getHealthDate());
                long fileNumber = new SnowflakeUtils().nextId();
                report.setFileNumber(fileNumber);
                Setmeal setmeal = setmealService.getById(appointment.getSetmealId());
                String setmealName = setmeal.getName();
                report.setSetmealName(setmealName);
                String nowTime = getNowTime();
                report.setUploadTime(nowTime);

                boolean reportFlag = reportService.save(report);

                if (reportFlag){

                    Integer oid = order.getId();
                    Integer rid = report.getId();
                    boolean flagOR = orderService.addOR(oid, rid);

                    if (flagOR){
                        java.sql.Date healthDate = appointment.getHealthDate();
                        Ordersetting ordersetting = ordersettingService.findByOrderDate(healthDate);
                        boolean flag3 = ordersettingService.updateReservations(healthDate, ordersetting.getVersion());
                        if (flag3){
                            return Result.success();
                        }else {
                            return Result.fail("预约数量修改失败");
                        }
                    }
                }
            }
                return Result.fail("添加失败");
        }else {
            // 不是会员 --- 添加到 appointment表
            appointment.setMemberId(memberID);
            boolean flag = appointmentService.addAppointment(appointment);
            if (flag){

                Report report = new Report();
                report.setMemberId(memberID);
                report.setName(appointment.getName());
                report.setSex(appointment.getSex());
                report.setHealthDate(appointment.getHealthDate());
                long fileNumber = new SnowflakeUtils().nextId();
                report.setFileNumber(fileNumber);
                Setmeal setmeal = setmealService.getById(appointment.getSetmealId());
                String setmealName = setmeal.getName();
                report.setSetmealName(setmealName);
                String nowTime = getNowTime();
                report.setUploadTime(nowTime);

                report.setAppointmentId(appointment.getId());

                System.out.println(report);
                boolean flag2 = reportService.save(report);

                boolean flag3;
                if (flag2){
                    java.sql.Date healthDate = appointment.getHealthDate();
                    Ordersetting ordersetting = ordersettingService.findByOrderDate(healthDate);
                    flag3 = ordersettingService.updateReservations(healthDate,ordersetting.getVersion());
                    if (flag3){
                        return Result.success();
                    }else {
                        return Result.fail("预约数量修改失败");
                    }
                }
            }
                return Result.fail("添加失败");
        }
    }

    private String getNowTime(){
        Date d = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(d);
    }
}
